/*
* $Id: UIXmlManager.java 272 2011-02-21 05:37:35Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import apiary.Apiary;

import apiary.component.journal.Wizard;

public class UIXmlManager
{
    public static String TYPE_ID = "id";
    public static String TYPE_FORM = "form";
    public static String TYPE_WIZARD = "wizard";

    private class ItemNextForm
    {
        public String type;
        public Hashtable params;
    }
    private class UIXmlHandler extends DefaultHandler
    {
        private String idForm;
        private String idItem;

        public void startElement(String uri, String localName,
                                 String qName, Attributes attributes)
                                 throws SAXException {
            if(qName.equals("f")) {
                idForm = attributes.getValue("id");
                ApiaryForm form = getNewForm(attributes.getValue("n"));
                forms.put(idForm, form);
            } else if(qName.equals("cm")) {
                ApiaryForm form = (ApiaryForm) forms.get(idForm);
                form.addCommand(attributes.getValue("t"),
                                attributes.getValue("n"));
            } else if(qName.equals("i")) {
                idItem = attributes.getValue("id");
                ApiaryForm form = (ApiaryForm) forms.get(idForm);
                form.AddItem(attributes.getValue("n"),
                             attributes.getValue("img"),
                             idItem);
                ItemNextForm nextForm = getNewItemNextForm();
                nextForms.put(idItem, nextForm);
            } else if(qName.equals("nf")) {
                ItemNextForm nextForm = (ItemNextForm) nextForms.get(idItem);
                nextForm.type = attributes.getValue("t");
            } else if(idItem != null) {
                ItemNextForm nextForm = (ItemNextForm) nextForms.get(idItem);
                nextForm.params.put(qName, attributes.getValue("v"));
            }
        }

        public void endElement(String uri, String localName,
                               String qName) throws SAXException {
            if(qName.equals("f")) {
                idForm = null;
            } else if(qName.equals("i")) {
                idItem = null;
            }
        }
    }

    private SAXParser saxParser;
    private Hashtable forms;
    private Hashtable nextForms;

    public UIXmlManager() {
        forms = new Hashtable();
        nextForms = new Hashtable();
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            saxParser = factory.newSAXParser();
            InputStream file = getClass().getResourceAsStream("/res/main_ui.xml");
            saxParser.parse(file, new UIXmlHandler());
        } catch (IOException ex) {
            System.err.println(ex.getMessage());
        } catch (ParserConfigurationException ex) {
            System.err.println(ex.getMessage());
        } catch (SAXException ex) {
            System.err.println(ex.getMessage());
        }
    }

    public BackScreen getFirstForm() {
        return (BackScreen) forms.get("main");
    }

    public BackScreen getNextForm(String id) {
        try {
            ItemNextForm nextForm = (ItemNextForm) nextForms.get(id);
            if(nextForm.type.equals(TYPE_ID))
                return (BackScreen) forms.get(nextForm.params.get("id"));
            if(nextForm.type.equals(TYPE_FORM)) {
                String nameClass = (String) nextForm.params.get("cl");
                BackScreen form = (BackScreen) Class.forName(nameClass).newInstance();
                form.setTitle((String) nextForm.params.get("n"));
                return form;
            }
            if(nextForm.type.equals(TYPE_WIZARD)) {
                String nameClass = (String) nextForm.params.get("cl");
                Wizard wizard = (Wizard) Class.forName(nameClass).newInstance();
                String backFormId = (String) nextForm.params.get("bfid");
                BackScreen backForm = (BackScreen) forms.get(backFormId);
                wizard.setCancelForm(backForm);
                return wizard.getFirstForm();
            }
        } catch (ClassNotFoundException ex) {
            System.err.println(ex.getMessage());
        } catch (IllegalAccessException ex) {
            System.err.println(ex.getMessage());
        } catch (InstantiationException ex) {
            System.err.println(ex.getMessage());
        }
        return null;
    }

    private ApiaryForm getNewForm(String name) {
        return new ApiaryForm(Apiary.getInstance().getLocaleString(name), this);
    }

    private ItemNextForm getNewItemNextForm() {
        ItemNextForm nextForm = new ItemNextForm();
        nextForm.params = new Hashtable();
        return nextForm;
    }
}
